package com.zg.system.soc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.zg.common.core.R;
import com.zg.common.web.Req;
import com.zg.common.core.User;
import com.zg.common.core.dao.auth.AuthUser;
import com.zg.common.core.dao.auth.AuthUserDao;
import com.zg.common.core.dao.soc.SocPraise;
import com.zg.common.core.dao.soc.SocPraiseDao;
import com.zg.common.core.util.JsonUtil;
import com.zg.system.soc.vo.ObjVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Zxb
 */
@RestController
@AllArgsConstructor
@Api(tags = {"点赞功能"})
public class PraiseController {

    private AuthUserDao userDao;
    private SocPraiseDao praiseDao;

    @GetMapping("/praise/count")
    @ApiOperation("统计赞数量")
    public R<?> count(String objType, String objKey) {
        long count = praiseDao.count(
                new LambdaQueryWrapper<SocPraise>()
                        .eq(SocPraise::getObjType, objType)
                        .eq(SocPraise::getObjKey, objKey)
        );
        return R.success(String.valueOf(count));
    }

    @GetMapping("/praise/user")
    @ApiOperation("获取点赞用户列表")
    public R<?> listUser(String objType, String objKey) {
        List<SocPraise> list = praiseDao.list(
                new LambdaQueryWrapper<SocPraise>()
                        .eq(SocPraise::getObjType, objType)
                        .eq(SocPraise::getObjKey, objKey)
                        .orderByAsc(SocPraise::getCreateTime));
        List<Long> userIdList = list.stream().map(SocPraise::getUserId).collect(Collectors.toList());
        List<AuthUser> userList = userDao.list(
                new LambdaQueryWrapper<AuthUser>()
                        .in(AuthUser::getId, userIdList)
        );
        return R.success(userList);
    }

    @GetMapping("/praise/search")
    @ApiOperation("判断当前用户是否点赞")
    public R<?> search(String objType, String objKey) {
        User u = Req.getCurrUser();
        if (u == null) {
            return R.success(false);
        }
        long count = praiseDao.count(
                new LambdaQueryWrapper<SocPraise>()
                        .eq(SocPraise::getObjType, objType)
                        .eq(SocPraise::getObjKey, objKey)
                        .eq(SocPraise::getUserId, u.getId()));
        boolean hasPraise = count > 0;
        return R.success(hasPraise);
    }

    @PostMapping("/praise/save")
    @ApiOperation("保存赞")
    public R<?> save(@RequestBody @Valid ObjVo objVo) {
        String objType = objVo.getObjType();
        String objKey = objVo.getObjKey();
        User u = Req.getCurrUser();
        SocPraise praise = praiseDao.getOne(
                new LambdaQueryWrapper<SocPraise>()
                        .eq(SocPraise::getObjType, objType)
                        .eq(SocPraise::getObjKey, objKey)
                        .eq(SocPraise::getUserId, u.getId()));
        ObjectNode on = JsonUtil.on();
        if (praise == null) {
            praise = new SocPraise();
            praise.setObjType(objType);
            praise.setObjKey(objKey);
            praise.setUserId(u.getId());
            praiseDao.save(praise);
            on.put("hasPraise", true);
        } else {
            praiseDao.removeById(praise);
            on.put("hasPraise", false);
        }
        long count = praiseDao.count(
                new LambdaQueryWrapper<SocPraise>()
                        .eq(SocPraise::getObjType, objType)
                        .eq(SocPraise::getObjKey, objKey));
        on.put("count", count);
        return R.success(on);
    }
}
